package cs429_project.clustering;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Pairs up a term in a document with its frequency.  Used to create vectors
 * for k-means clustering.
 * @deprecated Now using maps to do what this class did.
 * @author Peter Ferrans
 */
public class TermAndFreq implements Comparable<TermAndFreq> {
    /** Default scale for the frequency. */
    public static final int DEFAULT_FREQ_SCALE = 10;
    /** Default rounding mode for the frequency */
    public static final RoundingMode DEFAULT_FREQ_ROUND_MODE = RoundingMode.UP;
    /** The term in this pairing. */
    public String term;
    /** The frequency in this pairing. */
    public BigDecimal frequency;

    /**
     * Default constructor.  Creates pairing with the term as an empty String
     * and a frequency of 0.
     */
    public TermAndFreq() {
        this.term = "";
        this.frequency = BigDecimal.ZERO;
        setDefaultFrequencyScale();
    }

    /**
     * Creates a new pairing with the values passed in through the parameters.
     * @param term
     *    The term for the new pairing.
     * @param frequency
     *    The frequency for the new pairing.  The scale for this frequency is
     *    whichever scale the frequency had when it was passed in, but the
     *    rounding mode is {@link java.math.RoundingMode#UP}.
     */
    public TermAndFreq(String term, BigDecimal frequency) {
        this.term = term;
        this.frequency = 
                frequency.setScale(frequency.scale(), DEFAULT_FREQ_ROUND_MODE);
    }

    /**
     * Sets the scale for the frequency in this object.
     * @param scale
     *    The scale (number of digits after the decimal point) for the
     *    frequency.
     * @param roundingMode
     *    Mode for rounding the frequency when necessary.
     */
    public final void setFrequencyScale(int scale, RoundingMode roundingMode) {
        this.frequency = this.frequency.setScale(scale, roundingMode);
    }

    @Override
    public String toString() {
        String retStr = "[TERM:" + this.term + ", FREQUENCY:" +
                this.frequency + "]";

        return retStr;
    }

    /**
     * Compares this instance with another instance of TermAndFreq.  The
     * objects are compared by their terms; the greater object is the one whose
     * term comes alphabetically <em>after</em> the other.  All comparisons
     * between Strings are case-insensitive here.
     * @param other
     *    The other instance of TermAndFreq for comparison.
     * @return
     *    A number less than, greater than, or equal to 0 if this instance is
     *    less than, greater than, or equal to {@code other}.
     */
    @Override
    public int compareTo(TermAndFreq other) {
        String thisLC = this.term.toLowerCase();
        String otherLC = other.term.toLowerCase();
        int result = thisLC.compareTo(otherLC);

        return result;
    }

    private void setDefaultFrequencyScale() {
        this.setFrequencyScale(DEFAULT_FREQ_SCALE, DEFAULT_FREQ_ROUND_MODE);
    }
}
